package com.ftdigital.primemovers_operators.fragment;


import android.app.AlertDialog;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.ftdigital.primemovers_operators.MainActivity;
import com.ftdigital.primemovers_operators.MyApplication;
import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.config.GlobalData;
import com.ftdigital.primemovers_operators.config.RequestAddr;
import com.ftdigital.primemovers_operators.model.AddOilInfo;
import com.ftdigital.primemovers_operators.model.BaseModel;
import com.ftdigital.primemovers_operators.model.Order;
import com.ftdigital.primemovers_operators.util.http.AjaxCallBack;
import com.google.gson.JsonSyntaxException;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 */
@EFragment(R.layout.fragment_en_sure)
public class EnSureFragment extends BaseFragment {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @FragmentArg("order_no")
    String order_no;

    @ViewById(R.id.password)
    EditText password;

    EnSureFragment enSureFragment;

    @AfterViews
    @Override
    void initView() {
        enSureFragment = this;
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.title_please_input_pass));
    }

    @Click({R.id.back_btn, R.id.submit})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
            case R.id.submit:
                if (password.getText().toString().equals("")) {
                    Toast.makeText(getActivity(), "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                submitData();
                break;
        }
    }

    private void submitData() {
        requestParams = "order_no=" + order_no
                + "&pwd=" + password.getText().toString().trim()
                + "&supplier_id=" + MyApplication.userInfo.getData().getSupplier_id()
                + "&user_id=" + MyApplication.userInfo.getData().getUser_id();
        requestUrl = RequestAddr.doneOrder + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });

    }


    private void dealData(Object object) {
        try {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
        } catch (JsonSyntaxException e) {
            return;
        }
        Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
        if (!baseModel.isFlag()) {
            return;
        }
        showSelect();
    }


    AlertDialog alertDialog;

    private void showSelect() {
        alertDialog = new AlertDialog.Builder(MainActivity.getInstance()).create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.noticy_dialog);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        TextView backBtn = (TextView) window.findViewById(R.id.backbtn);
        TextView sureInfo = (TextView) window.findViewById(R.id.sureinfo);
        backBtn.setText("查看详情");
        sureInfo.setText("前往评价");
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
                MainActivity.getInstance().getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                MainActivity.getInstance().tabHost.setCurrentTab(1);
//                MainActivity.getInstance().switchFragment(enSureFragment, EnSureFragment_.builder().build());
            }
        });
        sureInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
                MainActivity.getInstance().getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                MainActivity.getInstance().tabHost.setCurrentTab(1);
//                MainActivity.getInstance().switchFragment(enSureFragment, EnSureFragment_.builder().build());
            }
        });
    }

}
